package com.alipay.sofa.award.biz.service.config;

import com.alibaba.common.lang.StringUtil;
import com.alipay.drm.client.DRMClient;
import com.alipay.drm.client.api.annotation.DAttribute;
import com.alipay.drm.client.api.annotation.DObject;
import com.alipay.sofa.award.biz.service.facade.ActivityAwardFacade;
import com.alipay.sofa.award.biz.service.facade.ActivityTimeFacade;
import com.alipay.sofa.award.common.dal.dataobject.Award;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;

/**
 * DynamicConfigAward
 *
 * {
        "rank": "1",
        "totalCount": 5,
        "awardContent": "T 恤",
        "awardDesc": "一等奖"
  }
 * @author yangguanchao
 * @since 2018/09/04
 */
@DObject(region = "AntCloud", appName = "award-core", id = "com.alipay.sofa.award.biz.service.config.DynamicConfigAward")
public class DynamicConfigAward {

    private static final Logger logger              = LoggerFactory
                                                        .getLogger(DynamicConfigAward.class);

    private ObjectMapper        jacksonObjectMapper = new ObjectMapper();

    @Autowired
    private ActivityAwardFacade activityAwardFacade;

    @Autowired
    private ActivityTimeFacade  activityTimeFacade;

    @DAttribute
    private String              awardJsonString;

    @DAttribute
    private String              updatedTimeJsonString;

    public void init() {
        DRMClient.getInstance().register(this);
        logger
            .info("com.alipay.sofa.award.biz.service.config.DynamicConfigAward.init() is running");
    }

    public String getAwardJsonString() {
        return awardJsonString;
    }

    /**
     *
     * Dynamic config award
     * @param awardJsonString award json string
     */
    public void setAwardJsonString(String awardJsonString) {
        if (StringUtil.isNotBlank(awardJsonString)) {
            this.awardJsonString = awardJsonString;
            logger
                .info("com.alipay.sofa.award.biz.service.config.DynamicConfigAward.setAwardJsonString is called.And awardJsonString = ["
                      + this.awardJsonString + "]");
            //update award db
            Award dynamicAward = null;
            try {
                dynamicAward = this.jacksonObjectMapper.readValue(awardJsonString, Award.class);
            } catch (IOException e) {
                logger.error("setAwardJsonString by DDCS error!", e);
            }
            if (dynamicAward != null) {
                //update total count equals award count
                int totalCount = dynamicAward.getTotalCount();
                Integer awardCount = dynamicAward.getAwardCount();
                if (awardCount == null || totalCount != awardCount) {
                    dynamicAward.setAwardCount(totalCount);
                }
                this.activityAwardFacade.updateAwardByRankWithSameCount(dynamicAward);
            }
        }
    }

    public String getUpdatedTimeJsonString() {
        return updatedTimeJsonString;
    }

    public void setUpdatedTimeJsonString(String updatedTimeJsonString) {
        if (StringUtil.isNotBlank(updatedTimeJsonString)) {
            this.updatedTimeJsonString = updatedTimeJsonString;
            logger
                .info("com.alipay.sofa.award.biz.service.config.DynamicConfigAward.setUpdatedTimeJsonString is called.And updatedTimeJsonString = ["
                      + this.updatedTimeJsonString + "]");
            UpdatedTimeModel updatedTimeModel = null;
            try {
                updatedTimeModel = this.jacksonObjectMapper.readValue(this.updatedTimeJsonString,
                    UpdatedTimeModel.class);
            } catch (IOException e) {
                logger.error("setUpdatedTimeJsonString by DDCS error!", e);
            }
            if (updatedTimeModel != null) {
                String oldActivityTime = updatedTimeModel.getOldActivityTime();
                String updatedActivityTime = updatedTimeModel.getUpdatedActivityTime();
                if (StringUtil.isNotBlank(oldActivityTime)
                    && StringUtil.isNotBlank(updatedActivityTime)) {
                    this.activityTimeFacade
                        .updateActivityTime(oldActivityTime, updatedActivityTime);
                } else {
                    logger.error("UpdatedTimeModel has empty field, and "
                                 + updatedTimeModel.toString());
                }
            }
        }
    }

    public static class UpdatedTimeModel {

        private String oldActivityTime;

        private String updatedActivityTime;

        public String getOldActivityTime() {
            return oldActivityTime;
        }

        public String getUpdatedActivityTime() {
            return updatedActivityTime;
        }

        public void setOldActivityTime(String oldActivityTime) {
            this.oldActivityTime = oldActivityTime;
        }

        public void setUpdatedActivityTime(String updatedActivityTime) {
            this.updatedActivityTime = updatedActivityTime;
        }

        @Override
        public String toString() {
            return "UpdatedTimeModel{" + "oldActivityTime='" + oldActivityTime + '\''
                   + ", updatedActivityTime='" + updatedActivityTime + '\'' + '}';
        }
    }

}
